package com.naiterui.ehp.bs.patient.repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.PatientInquirerInfo;

/**
 * @Description 就诊人
 * @Author gongbaoqiang
 * @Date 2020/7/9 3:37 PM
 */
@Repository
public interface IPatientInquirerRepository extends BaseJpaRepository<PatientInquirerInfo, Long> {


    /**
     * 查询患者关联就诊人（未删除）
     * @param patientId
     * @return
     */
    List<PatientInquirerInfo> findByPatientIdAndStatus(Long patientId, Integer status);

    PatientInquirerInfo findByIdAndPlatform(Long id, Integer platform);

    /**
     * 根据患者id和与就诊人关系查询就诊人
     * @param patientId
     * @param relation
     * @return
     */
    List<PatientInquirerInfo> findByPatientIdAndRelation(Long patientId, Integer relation);

    /**
     * 根据患者和就诊人姓名查询
     * @param patientId
     * @param name
     * @return
     */
    PatientInquirerInfo findByPatientIdAndName(Long patientId, String name);

    PatientInquirerInfo findByPatientIdAndNameAndIdCard(Long patientId, String name, String idCard);

    List<PatientInquirerInfo> findByIdIn(Collection<Long> id);

    /**
     * 根据患者id和与就诊人关系查询就诊人
     * @param patientId
     * @param relation
     * @return
     */
    PatientInquirerInfo findFirstByPatientIdAndRelation(Long patientId, Integer relation);

    PatientInquirerInfo findFirstByPatientIdAndRelationAndStatus(Long patientId, Integer relation, Integer status);

    /**
     * 查询患者关联就诊人（包含已删除）
     * @param patientId
     * @return
     */
    List<PatientInquirerInfo> findByPatientId(Long patientId);


    /**
     * 根据身份证号查询就诊人列表
     *
     * @param idCard
     * @return
     */
    List<PatientInquirerInfo> findByIdCard(String idCard);

    PatientInquirerInfo findByPatientIdAndId(Long patientId, Long id);

    // 添加患者就诊人信息
    @Modifying
    @Query(value = "insert into patient_inquirer_info (id, patient_id, relation, name, id_card, gender, birthday, phone, address, contact_name, contact_phone, is_default," +
            " platform, account_status, created_at) values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15)",
            nativeQuery = true)
    public void savePatientInquirer(Long inquirerId, Long patientId, Integer relation, String name, String idCard, Integer gender, String birthday, String phone, String address,
           String contactName, String contactPhone, Integer isDefault, Integer platform, Integer accountStatus, Date createdAt);

    // 修改患者就诊人信息
    @Modifying
    @Query(value = "update patient_inquirer_info set relation = ?1, name = ?2, id_card = ?3, phone = ?4, address = ?5, contact_name = ?6, contact_phone = ?7," +
            " is_default = ?8, gender = ?9, birthday = ?10 where id = ?11 and patient_id = ?12", nativeQuery = true)
    public void updatePatientInquirer(Integer relation, String name, String idCard, String phone, String address, String contactName, String contactPhone,
           Integer isDefault, Integer gender, String birthday, Long inquirerId, Long patientId);

    // 删除就诊人
    @Modifying
    @Query(value = "update patient_inquirer_info set status = ?1 where id = ?2 and patient_id = ?3", nativeQuery = true)
    public void deletePatientInquirer(Integer status, Long inquirerId, Long patientId);
}
